<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/8
 * Time: 19:25
 */

namespace app\api\controller;

use app\api\Service\GameService;
use app\common\controller\Api;
use app\common\model\Bets;
use think\Cache;
use think\captcha\Captcha;
use think\Db;
use think\Request;

class Game extends Api
{
    protected $noNeedLogin = ['lotteryToLOL','lotteryToDOTA2','lotteryToPVP','gamelist','zjrecords','heroinfo','broadcast'];
    protected $noNeedRight = ['*'];
    protected $gameService;

    public function __construct($name = null)
    {
        parent::__construct($name);
        $this->gameService = new GameService();
    }

    /**
     * lol 开奖
     */
    public function lotteryToLOL()
    {
        $this->gameService->lottery('LOL');
    }
    /**
     * dota2 开奖
     */
    public function lotteryToDOTA2()
    {
        $this->gameService->lottery('DOTA2');
    }
    /**
     * pvp 开奖
     */
    public function lotteryToPVP()
    {
        $this->gameService->lottery('PVP');
    }

    public function gamelist( Request $request )
    {
        $game = $request->get('game');
        $model = '\app\common\model\Game' . ucfirst($game);
        $lists = $model::all();
        $this->success('成功',$lists);
    }

    /**
     * 最新中奖记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function zjrecords()
    {
        $bets = Bets::with('user')->where('status',1)
            ->order('id desc')
            ->limit(8)
            ->field('user_id,zj_money,status,game,id')
            //->group('user_id')
            ->select();
        $this->success('成功',$bets);
    }

    public function heroinfo( Request $request )
    {
        $game = $request->get('game');
        $index = $request->get('identifier');
        $model = '\app\common\model\Game' . ucfirst($game);
        $info = $model::where('identifier',$index)->find();
        $this->success('成功',$info);
    }

    /**
     * 开奖广播
     */
    public function broadcast()
    {
        $this->success('成功',[
            'dota2' => $this->gameService->getLotteries('dota2')[0],
            'pvp' => $this->gameService->getLotteries('pvp')[0],
            'lol' => $this->gameService->getLotteries('lol')[0],
        ]);
    }

    /**
     * 检测房间权限
     * @param GameService $gameService
     * @param string $roomlevel
     */
    public function checkRoomAuth(GameService $gameService,$roomlevel = '')
    {
        $money = (float)$this->auth->wallet->money;
        $levelAuth = $gameService->checkRoomAuth($money);
        $roomLevel =  $roomlevel ? $roomlevel :$this->request->get('roomlevel');
        if( in_array($roomLevel,$levelAuth) ){
            $this->success('ok');
        }else{
            $this->error('fail');
        }
    }
}